//
//  TabbarController.swift
//  BoostTestIOS
//
//  Created by 张海南 on 2023/4/17.
//

import UIKit

private enum TabbarItem: Int, CaseIterable {
    /// index必须按顺讯
    case main = 0
    case mine = 1

    var selectColor: UIColor {
        UIColor(hexString: "#FFFFFF") ?? .black
    }

    var normalColor: UIColor {
        UIColor(hexString: "#AFB4BE") ?? .white
    }

    var title: String {
        switch self {
        case .main: return "首页"
        case .mine: return "我的"
        }
    }

    var imageName: String {
        switch self {
        case .main: return "discover_normal"
        case .mine: return "mine_normal"
        }
    }

    var selectImageName: String {
        switch self {
        case .main: return "discover_selected"
        case .mine: return "mine_selected"
        }
    }
}


class TabbarController: UITabBarController {
    
    lazy var tabbar = TabBar()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        configureTabBar()
        addChilds()
    }

    private func configureTabBar() {
        setValue(tabbar, forKey: "tabBar")
        UITabBar.appearance().isTranslucent = false
        UITabBar.appearance().shadowImage = UIImage()
        UITabBar.appearance().backgroundImage = UIImage()
        UITabBar.appearance().backgroundColor = UIColor.white
        UITabBar.appearance().tintColor = UIColor(hexString: "#0061b0")
        UITabBar.appearance().unselectedItemTintColor = UIColor(hexString: "#000000")
    }
    
    /// 添加Chlid
    private func addChilds() {
        // 首页
        let homeVC = HomeViewController()
        // 我的
        let mineVC = UserViewController()
        
        let disNavi = getChild(homeVC,
                           title: TabbarItem.main.title,
                           imageNamed: TabbarItem.main.imageName,
                           selectedImageNamed: TabbarItem.main.selectImageName)
        addChild(disNavi)
    
        let mineNavi = getChild(mineVC,
                               title: TabbarItem.mine.title,
                               imageNamed: TabbarItem.mine.imageName,
                               selectedImageNamed: TabbarItem.mine.selectImageName)
        addChild(mineNavi)

    }
    
    private func getChild(_ childController: UIViewController,
                          title: String,
                          imageNamed: String,
                          selectedImageNamed: String) -> UINavigationController
    {
        childController.tabBarItem.image = UIImage(named: imageNamed)?.withRenderingMode(.alwaysOriginal)
        childController.tabBarItem.selectedImage = UIImage(named: selectedImageNamed)?.withRenderingMode(.alwaysOriginal)
        childController.tabBarItem.title = title
        return MainNavigationController(rootViewController: childController)
    }

}

class TabBar: UITabBar {

    init() {
        super.init(frame: .zero)
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        return super.hitTest(point, with: event)
    }
}


public extension UIColor {
    
    convenience init?(hexString: String, transparency: CGFloat = 1) {
        var string = ""
        if hexString.lowercased().hasPrefix("0x") {
            string =  hexString.replacingOccurrences(of: "0x", with: "")
        } else if hexString.hasPrefix("#") {
            string = hexString.replacingOccurrences(of: "#", with: "")
        } else {
            string = hexString
        }

        if string.count == 3 { // convert hex to 6 digit format if in short format
            var str = ""
            string.forEach { str.append(String(repeating: String($0), count: 2)) }
            string = str
        }

        guard let hexValue = Int(string, radix: 16) else { return nil }

        var trans = transparency
        if trans < 0 { trans = 0 }
        if trans > 1 { trans = 1 }

        let red = (hexValue >> 16) & 0xff
        let green = (hexValue >> 8) & 0xff
        let blue = hexValue & 0xff
        self.init(red: red, green: green, blue: blue, transparency: trans)
    }
    
    convenience init?(red: Int, green: Int, blue: Int, transparency: CGFloat = 1) {
        guard red >= 0 && red <= 255 else { return nil }
        guard green >= 0 && green <= 255 else { return nil }
        guard blue >= 0 && blue <= 255 else { return nil }

        var trans = transparency
        if trans < 0 { trans = 0 }
        if trans > 1 { trans = 1 }

        self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: trans)
    }
    
    convenience init?(hex: Int, transparency: CGFloat = 1) {
        var trans = transparency
        if trans < 0 { trans = 0 }
        if trans > 1 { trans = 1 }

        let red = (hex >> 16) & 0xff
        let green = (hex >> 8) & 0xff
        let blue = hex & 0xff
        self.init(red: red, green: green, blue: blue, transparency: trans)
    }
    
}
